package com.liuzhengting.security;

import com.liuzhengting.security.interceptor.CustomizeAuthenticationEntryPoint;
import com.liuzhengting.security.interceptor.CustomizeAuthenticationFailureHandler;
import com.liuzhengting.security.interceptor.CustomizeAuthenticationSuccessHandler;
import com.liuzhengting.security.interceptor.CustomizeLogoutSuccessHandler;
import com.liuzhengting.security.service.impl.UserDetailsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

@Configuration
public class WebSecurityConfg extends WebSecurityConfigurerAdapter {
	
	//登录成功处理逻辑
	@Autowired
	CustomizeAuthenticationSuccessHandler authenticationSuccessHandler;
	
	//登录失败处理逻辑
	@Autowired
	CustomizeAuthenticationFailureHandler authenticationFailureHandler;
	
	//匿名用户访问无权限资源时的异常
	@Autowired
	CustomizeAuthenticationEntryPoint authenticationEntryPoint;
	
	//登出成功处理逻辑
	@Autowired
	CustomizeLogoutSuccessHandler logoutSuccessHandler;

	@Bean
	public BCryptPasswordEncoder passwordEncoder() {
		// 设置默认的加密方式（强hash方式加密）
		return new BCryptPasswordEncoder();
	}
	
	@Bean
	public UserDetailsService userDetailsService() {
		//获取用户账号密码及权限信息
		return new UserDetailsServiceImpl();
	}
	
	
	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		System.out.println("123");
		auth.userDetailsService(userDetailsService()).passwordEncoder(new BCryptPasswordEncoder());
		System.out.println("123");
	}
	
	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.cors().and().csrf().disable();
		http.logout().permitAll()
				.logoutSuccessHandler(logoutSuccessHandler)
				.deleteCookies("JSESSIONID")
				.and().formLogin().permitAll()
				.successHandler(authenticationSuccessHandler)
				.failureHandler(authenticationFailureHandler)
				.and().sessionManagement().maximumSessions(1);
		super.configure(http);
	}
	
//	.logout().permitAll()
//				.logoutSuccessHandler(logoutSuccessHandler)
//				.deleteCookies("JSESSIONID")
//				.and()

//
//	@GetMapping("/404")
//	public String notFoundPage() {
//		return "404";
//	}
//
//	@GetMapping("/403")
//	public String accessError() {
//		return "403";
//	}
//
//	@GetMapping("/500")
//	public String internalError() {
//		return "500";
//	}
//
//	@GetMapping("/success")
//	@ResponseBody
//	public String success(){
//		return "认证成功,进入success成功";
//	}
//
//	@GetMapping(value = "/user/login")
//	private String loginPage(){
//		return "login";
//	}
//
//	@GetMapping(value = "/person")
//	public String personPage(){
//		return "person";
//	}
//
//	@GetMapping(value = "/admin/index")
//	public String adminPage(){
//		return "admin/admin";
//	}

}
